/*-
 * #%L
 * HAPI FHIR JPA Server - HFQL Driver
 * %%
 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package ca.uhn.fhir.jpa.fql.jdbc;

import ca.uhn.fhir.jpa.fql.executor.IHfqlExecutionResult;
import ca.uhn.fhir.jpa.fql.util.HfqlConstants;
import org.hl7.fhir.r4.model.Parameters;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

import static ca.uhn.fhir.jpa.fql.jdbc.JdbcConnection.newSqlExceptionForUnsupportedOperation;

class JdbcStatement implements Statement {
	private final JdbcConnection myConnection;
	private int myMaxRows;
	private int myFetchSize = HfqlConstants.DEFAULT_FETCH_SIZE;
	private JdbcResultSet myResultSet;

	public JdbcStatement(JdbcConnection theConnection) {
		myConnection = theConnection;
	}

	@Override
	public ResultSet executeQuery(String theSqlText) throws SQLException {
		execute(theSqlText);
		return getResultSet();
	}

	@Override
	public int executeUpdate(String sql) throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public void close() {
		// ignored
	}

	@Override
	public int getMaxFieldSize() {
		return 0;
	}

	@Override
	public void setMaxFieldSize(int max) {
		// ignored
	}

	@Override
	public int getMaxRows() {
		return myMaxRows;
	}

	@Override
	public void setMaxRows(int theMaxRows) {
		myMaxRows = theMaxRows;
	}

	@Override
	public void setEscapeProcessing(boolean enable) {
		// ignored
	}

	@Override
	public int getQueryTimeout() {
		return 0;
	}

	@Override
	public void setQueryTimeout(int seconds) {
		// ignored
	}

	@Override
	public void cancel() {
		// ignored
	}

	@Override
	public SQLWarning getWarnings() {
		return null;
	}

	@Override
	public void clearWarnings() {
		// ignored
	}

	@Override
	public void setCursorName(String name) {
		// ignored
	}

	@Override
	public boolean execute(String sql) throws SQLException {
		Integer limit = null;
		if (getMaxRows() > 0) {
			limit = getMaxRows();
		}

		int fetchSize = myFetchSize;

		Parameters input = HfqlRestClient.newQueryRequestParameters(sql, limit, fetchSize);
		IHfqlExecutionResult result = myConnection.getClient().execute(input, true, getFetchSize());

		myResultSet = new JdbcResultSet(result, this);
		return true;
	}

	@Override
	public ResultSet getResultSet() {
		return myResultSet;
	}

	@Override
	public int getUpdateCount() {
		return 0;
	}

	@Override
	public boolean getMoreResults() {
		return false;
	}

	@Override
	public int getFetchDirection() {
		return ResultSet.FETCH_FORWARD;
	}

	@Override
	public void setFetchDirection(int direction) {
		// ignored
	}

	@Override
	public int getFetchSize() {
		return myFetchSize;
	}

	@Override
	public void setFetchSize(int theFetchSize) {
		myFetchSize = theFetchSize;
	}

	@Override
	public int getResultSetConcurrency() {
		return ResultSet.CONCUR_READ_ONLY;
	}

	@Override
	public int getResultSetType() {
		return ResultSet.TYPE_FORWARD_ONLY;
	}

	@Override
	public void addBatch(String sql) throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public void clearBatch() throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public int[] executeBatch() {
		return new int[0];
	}

	@Override
	public Connection getConnection() {
		return myConnection;
	}

	@Override
	public boolean getMoreResults(int current) throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public ResultSet getGeneratedKeys() throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public int executeUpdate(String sql, String[] columnNames) throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public boolean execute(String sql, int[] columnIndexes) throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public boolean execute(String sql, String[] columnNames) throws SQLException {
		throw newSqlExceptionForUnsupportedOperation();
	}

	@Override
	public int getResultSetHoldability() {
		return ResultSet.CLOSE_CURSORS_AT_COMMIT;
	}

	@Override
	public boolean isClosed() {
		return false;
	}

	@Override
	public boolean isPoolable() {
		return false;
	}

	@Override
	public void setPoolable(boolean thePoolable) {
		// ignored
	}

	@Override
	public void closeOnCompletion() {
		// ignored
	}

	@Override
	public boolean isCloseOnCompletion() {
		return false;
	}

	@Override
	public <T> T unwrap(Class<T> theInterface) {
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> theInterface) {
		return false;
	}
}
